<?php

namespace App\Library\Service;

use App\Library\Logic\BuyWillLogic;
use App\Library\Requests\Base\SerialNumberRequest;
use App\Library\Requests\BuyWill\BuyWillAddRequest;
use App\Library\Requests\BuyWill\BuyWillRandRequest;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;

class BuyWillService extends BaseService
{
    public function __construct(BuyWillLogic $logic)
    {
        return $this->logic = $logic;
    }

    /**
     *
     * @param BuyWillAddRequest $request
     * @return JsonResponse
     *
     */
    public function add(BuyWillAddRequest $request): JsonResponse
    {
        $array = handleRequest($request);
        $array['title'] = isset($array['title']) ? htmlspecialchars(strip_tags(trim($array['title']))) : null;
        $array['description'] = isset($array['description']) ? htmlspecialchars(strip_tags(trim($array['description']))) : null;
        return success($this->logic->insertForModel(array_merge(['userId' => MAINID, 'addUserId' => USERID], $array)));
    }

    public function edit(BuyWillAddRequest $request): JsonResponse
    {
        $model = $this->logic->findBySerialNumber($request);
        if (is_null($model)) return fail(433);
        $model->title = htmlspecialchars(strip_tags(trim($request->title)));
        $model->description = htmlspecialchars(strip_tags(trim($request->description)));
        $model->file_id = $request->fileId;
        $model->price = !is_null($request->price) ? $request->price : 0;
        $model->original_price = !is_null($request->originalPrice) ? $request->originalPrice : 0;
        $model->stock = !is_null($request->stock) ? $request->stock : 0;
        $model->limit = !is_null($request->limit) ? $request->limit : 0;
        $model->save();
        return success();
    }

    /**
     *
     * @param SerialNumberRequest $request
     * @return JsonResponse
     *
     */
    public function transShelfStatus(SerialNumberRequest $request): JsonResponse
    {
        $model = $this->logic->findBySerialNumber($request);
        if (is_null($model)) return fail(433);
        if ($model->status) $model->unshelf_at = now();
        else $model->shelf_at = now();
        $model->status = $model->status ? 0 : 1;
        $model->save();
        return success();
    }

    /**
     *
     * @param SerialNumberRequest $request
     * @return JsonResponse
     *
     */
    public function delete(SerialNumberRequest $request): JsonResponse
    {
        $model = $this->logic->findBySerialNumber($request);
        if (is_null($model)) return fail(433);
        $model->deleted_at = now();
        $model->save();
        return success();
    }

    /**
     *
     * @param BuyWillRandRequest $request
     * @return JsonResponse
     */
    public function rand(BuyWillRandRequest $request): JsonResponse
    {
        $shopUserId = array_filter(explode('-', aesDecrypt($request->userId)), function ($v) {
            return $v !== '';
        });

        return success($this->logic->rand(implode(',', $shopUserId)));
    }
}
